package com.ccom.test.serviceImpl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ccom.api.domain.Article;
import com.ccom.api.domain.excel.ArticleExcel;
import com.ccom.core.common.exception.CcomBootException;
import com.ccom.core.common.util.BeanUtils;
import com.ccom.test.mapper.ArticleMapper;
import com.ccom.test.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @Description:
 * @author: jjzhang24
 * @date: 2021年12月23日
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public boolean submit(Article article) {
        Integer cnt = articleMapper.selectCount(Wrappers.<Article>query().lambda().eq(Article::getUserId, article.getUserId()).eq(Article::getTitle, article.getTitle()));
        if (cnt > 0) {
            throw new CcomBootException("当前文章已存在!");
        }
        return this.save(article);
    }

    @Override
    public void importUser(List<ArticleExcel> list) {
        list.forEach(articleExcel -> {
            Article article = Objects.requireNonNull(BeanUtils.copy(articleExcel, Article.class));
            this.submit(article);
        });
    }
}
